//
//  AppDataManager.m
//  LoveCar
//
//  Created by zhanshu on 14/11/20.
//  Copyright (c) 2014年 zhanshu. All rights reserved.
//

#import "AppDataManager.h"
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"

#define FMDBQuickCheck(SomeBool) { if (!(SomeBool)) { NSLog(@"Failure on line %d", __LINE__); abort(); } }
#define DATABASE_PATH [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0]stringByAppendingString:@"/Area.sqlite"]
//#define DATABASE_PATH [[NSBundle mainBundle] pathForResource:@"Area" ofType:@"sqlite"]
@implementation AppDataManager
//+(BOOL)saveNotificationMessWithID:(int)ID content:(NSString*)content time:(NSString*)time state:(NSString*)state userName:(NSString*)userName type:(int)type
+(FMDatabase*)openFMDataBase
{
     NSString *path=[[NSBundle mainBundle] pathForResource:@"Area" ofType:@"sqlite"];
    FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
    return db;
}
#pragma mark- 保存选择城市信息
+(BOOL)saveRecordOfLocationMess:(NSString*)address city:(NSString*)city lat:(long)lat log:(long)log
{
//     NSString *path=[[NSBundle mainBundle] pathForResource:@"Area" ofType:@"sqlite"];
//    NSLog(@"the path:%@",NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0]);
    FMDatabase *db = [self openFMDataBase];
    if (![db open]) {
        NSLog(@"数据库打开失败");
        return NO;
    };
    
    NSString *createStr=[NSString stringWithFormat:@"CREATE  TABLE  IF NOT EXISTS 'LocationMess' ('address' VARCHAR, 'city' VARCHAR, 'lat' INTEGER, 'log' INTEGER)"];
    
    BOOL worked = [db executeUpdate:createStr];
    FMDBQuickCheck(worked);
    NSString *insertStr=[NSString stringWithFormat:@"INSERT INTO 'LocationMess' (address,city,lat,log) VALUES ('%@','%@',%ld,%ld)",address,city,lat,log];
    worked = [db executeUpdate:insertStr];
    FMDBQuickCheck(worked);
    [db close];
    return worked;
}
#pragma mark- 保存周边的搜索信息
+(BOOL)saveNearSearchMess:(NSString *)searchString user:(NSString*)user
{
    FMDatabase *db = [self openFMDataBase];
    if (![db open]) {
        NSLog(@"数据库打开失败");
        return NO;
    };
    
    NSString *createStr=[NSString stringWithFormat:@"CREATE  TABLE  IF NOT EXISTS 'NearSearchMess' ('address' VARCHAR, 'user' VARCHAR)"];
    
    BOOL worked = [db executeUpdate:createStr];
    FMDBQuickCheck(worked);
    NSString *insertStr=[NSString stringWithFormat:@"INSERT INTO 'NearSearchMess' (address,user) VALUES ('%@','%@')",searchString,user];
    worked = [db executeUpdate:insertStr];
    FMDBQuickCheck(worked);
    [db close];
    return worked;
}
#pragma mark-  保存拍卖的搜索记录
+(BOOL)saveSaleSearchMess:(NSString*)searchString
{
    FMDatabase *db = [self openFMDataBase];
    if (![db open]) {
        NSLog(@"数据库打开失败");
        return NO;
    };
    
    NSString *createStr=[NSString stringWithFormat:@"CREATE  TABLE  IF NOT EXISTS 'SaleSearchRecord' ('address' VARCHAR)"];
    
    BOOL worked = [db executeUpdate:createStr];
    FMDBQuickCheck(worked);
    NSString *insertStr=[NSString stringWithFormat:@"INSERT INTO 'SaleSearchRecord' (address) VALUES ('%@')",searchString];
    worked = [db executeUpdate:insertStr];
    FMDBQuickCheck(worked);
    [db close];
    return worked;
}
+(BOOL)saveNearMerchantMess:(NSString*)searchString type:(int)type
{
    FMDatabase *db = [self openFMDataBase];
    if (![db open]) {
        NSLog(@"数据库打开失败");
        return NO;
    };
    
    NSString *createStr=[NSString stringWithFormat:@"CREATE  TABLE  IF NOT EXISTS 'NearMerchantSearch' ('address' VARCHAR,'type' INTEGER)"];
    
    BOOL worked = [db executeUpdate:createStr];
    FMDBQuickCheck(worked);
    NSString *insertStr=[NSString stringWithFormat:@"INSERT INTO 'NearMerchantSearch' (address,type) VALUES ('%@',%d)",searchString,type];
    worked = [db executeUpdate:insertStr];
    FMDBQuickCheck(worked);
    [db close];
    return worked;
}

//获取数据库中的消息内容
+(NSMutableArray*)getLocationMessRecordBySQLName:(NSString*)name
{
    FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
    if (![db open]) {
        NSLog(@"数据库打开失败");
        return NO;
    };
    //@"select * from message order by id desc"
    NSString *sql=[NSString stringWithFormat:@"select * from %@",name];
    FMResultSet *rs=[db executeQuery: sql];
    NSMutableArray *ary = [NSMutableArray array];
    while ([rs next]) {
        NSDictionary *dic = [rs resultDictionary];
        [ary addObject:dic];
    }
    [rs close];
    [db close];
    return ary;
}
+(BOOL)deleateAllMessBySQLName:(NSString*)name
{
    FMDatabase *db=[FMDatabase databaseWithPath:DATABASE_PATH];
    if (![db open]) {
        //打开数据库失败
        return NO;
    }
    NSString *s=[NSString stringWithFormat:@"delete from %@",name];
    BOOL worked=[db executeUpdate:s];
    [db close];
    return worked;
}
+(BOOL)deleateMessByAddressName:(NSString*)address BySQLName:(NSString*)name
{
    FMDatabase *db=[FMDatabase databaseWithPath:DATABASE_PATH];
    if (![db open]) {
        //打开数据库失败
        return NO;
    }
    NSString *s=[NSString stringWithFormat:@"delete from %@ where address='%@'",name,address];
    BOOL worked=[db executeUpdate:s];
    [db close];
    return worked;
}
//+(BOOL)cancleMessByIDwithSql:(int)ID
//{
//    FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
//    if (![db open]) {
//        NSLog(@"数据库打开失败");
//        return NO;
//    };
//    NSString *s=[NSString stringWithFormat:@"delete from NotificationMess where systemMessageId=%d",ID];
//    BOOL worked=[db executeUpdate:s];
//    [db close];
//    return worked;
//}
//+(BOOL)removeAllTheValue:(int)type
//{
//    FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
//    if (![db open]) {
//        NSLog(@"数据库打开失败");
//        return NO;
//    };
//    NSString *s=[NSString stringWithFormat:@"delete from NotificationMess where type = %d",type];
//    BOOL worked=[db executeUpdate:s];
//    [db close];
//    return worked;
//}
//+(BOOL)updataState:(int)type//更新状态
//{
//    FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
//    if (![db open]) {
//        NSLog(@"数据库打开失败");
//        return NO;
//    };
//    NSString *s=[NSString stringWithFormat:@"update NotificationMess set state=0 where type = %d",type];
//    BOOL worked=[db executeUpdate:s];
//    [db close];
//    return worked;
//}
//+(BOOL)updataStateByID:(int )ID//更新状态
//{
//    FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
//    if (![db open]) {
//        NSLog(@"数据库打开失败");
//        return NO;
//    };
//    NSString *s=[NSString stringWithFormat:@"update NotificationMess set state=1 where systemMessageId=%d",ID];
//    BOOL worked=[db executeUpdate:s];
//    [db close];
//    return worked;
//}



@end
